package com.collabscm.pf.web.taglib;


import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.collabscm.pf.exception.UncheckedException;
import com.collabscm.pf.util.ConstantUtils;

/**
 * 参数标签类
 * @author tony
 */
public class ParametersSelectTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	
	/**
	 * 参数类别
	 */
	private String paramCategory;
	
	/**
	 * select id
	 */
	private String id;
	
	/**
	 * select名称
	 */
	private String name;
	
	/**
	 * 样式
	 */
	private String cssClass;
	
	/**
	 * 选中某个
	 */
	private String selected;
	
	/**
	 * 是否disabled
	 */
	private String disabled;
	
	/**
	 * onchange 事件
	 */
	private String onChange;
	
	/**
	 * 是否需要空项
	 */
	private String isEmpty;
	
	public int doStartTag() throws JspException {
		StringBuffer selectBuffer = new StringBuffer();
		selectBuffer.append("<select  name=\"" + name + "\" ");
		if (id != null && !"".equals(id)) {
			selectBuffer.append("id=\"" + id + "\" ");
		}
		if (disabled != null && !"".equals(disabled) && "true".equals(disabled)) {
			selectBuffer.append("disabled=\"true\" ");
		}
		if (cssClass != null && !"".equals(cssClass)) {
			selectBuffer.append("class=\"" + cssClass + "\" ");
		}
		//onchange 事件
		if (onChange != null && !"".equals(onChange)) {
			selectBuffer.append("onchange=\"" + onChange + "\"() ");
		}
		selectBuffer.append("/>");
		if("true".equals(isEmpty)) {
			selectBuffer.append("<option value=\"\">请选择</option>");
		}
		System.out.println("察看 selectBuffer " + selectBuffer);
//		HashMap<String, Integer> selectMap = ConstantUtils.getConstantMap(paramCategory);
//		if (!selectMap.isEmpty()){
//			for (Iterator iter = selectMap.keySet().iterator(); iter.hasNext();) {
//				Object name = iter.next();
//				Object id = (Object)selectMap.get(name);
//				String checked = "";
//				if (selected != null && selected.equals(id)) {
//					checked = "selected=\"true\"";
//				}
//				selectBuffer.append("<option value=\""+id+"\" "+checked+">"+name+"</option>");
//			}
//		}
		selectBuffer.append("</select>");
		JspWriter out = (JspWriter) pageContext.getOut();
		try {
			out.print(selectBuffer);
		} catch (IOException e) {
			throw new UncheckedException("参数下拉框标签生成错误", e);
		}
		return EVAL_PAGE;
	} 
	
	/**
	 * 
	 * @param onChange
	 * @param selectBuffer
	 */
	protected void fillChange(StringBuffer selectBuffer) {
		selectBuffer.append("onchange=\"" + onChange + "\"() ");
	}

	public String getParamCategory() {
		return paramCategory;
	}

	public void setParamCategory(String paramCategory) {
		this.paramCategory = paramCategory;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getSelected() {
		return selected;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public String getDisabled() {
		return disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

	public String getOnChange() {
		return onChange;
	}

	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}

	public String getIsEmpty() {
		return isEmpty;
	}

	public void setIsEmpty(String isEmpty) {
		this.isEmpty = isEmpty;
	}
}
